import RankingAPI from '../../../common/api/ctn/ranking.api.js';
import doT from '../../../../vendor/template/doT.js';
import { DictionaryType, requestDict } from "../../../common/dictionary/dictionary-type.js";
import { uploadFile, requestFilePath, publicFileDraw, sliceFileName } from "../../../common/api/file-request.js";

(function (window, document, $) {
    let _vm, timeIsShow = false, pigFarmIsShow = false, _addressData = {}, list = [], time_interval = [], pigType = 0
    const pigFarmTemplate = doT.template($('#pigFarm-template').text());
    const datesTemplate = doT.template($('#dates-template').text());
    const listTemplate = doT.template($('#list-template').text());
    $(function () {
        $('body').attr('data-weui-theme', 'light'); //强制光明模式

        $(document).on("click", '.dropdown_item', function () {
            $(this).addClass('active').siblings().removeClass('active')
            $('.rankingContent').empty();
            // 关闭
            $('.dropdown').css('display', 'none')
            $('#pigFarm i,#times i').css('rotate', "0deg")
            // 控制显示和隐藏
            pigFarmIsShow = false
            timeIsShow = false
            // 点击时间列表
            if ($(this).data("type") == 0) {
                getTimes($(this).data("val"))
            } else {// 点击 猪场列表
                // 获取切换的猪场
                pigType = $(this).data("val");
                // 修改文字
                $('#pigTitle').text(_addressData[pigType]);
                // 全部时 返回所有数据
                if (pigType == 0) return $('.rankingContent').append(listTemplate(list));
                // 其他返回
                let data = list.filter(item => item?.company?.id == pigType) || []
                // 数据未0就不往下走了
                if (data.length == 0) return $('.rankingContent').empty()
                $('.rankingContent').append(listTemplate(data));
            }
        })

        _initCommand();
        _initClick();
    });

    //命令初始化
    const _initCommand = async () => {
        //公司查询
        let res = await RankingAPI.getOrgInfo('ooca', 10);
        res.unshift({ name: "全部", id: 0 })
        res.forEach(item => {
            _addressData[item.id] = item.name
            $('.pigFarm').empty().append(pigFarmTemplate(res));
        });
        // 获取时间
        time_interval = getHalfYear();
        $('.times').empty().append(datesTemplate(time_interval));
        $('#queryTime').text(time_interval[0].text)
        getTimes(time_interval[0].val)
    }

    // 获取数据
    const getTimes = async (data) => {
        list = await RankingAPI.postAuditformBillsPlist({"Period":data});
        RankingAPI.getAuditformBills(data).then(res => {
            //list = res.resultEntries;
            let data = list.filter((item, index) => {
                if (pigType == 0) return item
                else if (item.type == pigType) return item
            });
            $('.rankingContent').empty().append(listTemplate(data));
            $('.auditNum').text(res.auditCount)
            $('.failNum').text(res.failNum)
            $('.qualifiledNum').text(res.qualifiledNum)
        }).catch(error => {
            $(".auditNum,.failNum,.qualifiledNum").text('0')
        });
        $('#queryTime').text((time_interval.find(item => item.val == data)).text)
    }

    // 获取时间间隔
    const getHalfYear = () => {
        // 获取当前时间
        let data = new Date();
        // 获取年份
        let year = data.getFullYear();
        // 获取月份
        let mon = data.getMonth() + 2;
        let array = new Array();
        for (let i = 0; i < 6; i++) {
            mon = mon - 1;
            if (mon <= 0) {
                year = year - 1;
                mon = mon + 12;
            }
            if (mon < 10) {
                mon = "0" + mon;
            }
            array[i] = { val: `${year}${mon}`, text: `${year}年${mon}月` };
        }
        return array;
    }

    const _initClick = () => {
        // 时间
        $('#times').on("click", function () {
            // 旋转图标
            $(this).find('i').css('rotate', timeIsShow ? "0deg" : "90deg")
            // 显示 下拉框
            $(".times").css("display", timeIsShow ? 'none' : 'block')
            if (pigFarmIsShow) {
                // 关闭旁边的下拉框
                $(".pigFarm").css("display", 'none')
                $('#pigFarm i').css('rotate', "0deg")
                pigFarmIsShow = false
            }
            // 点击改变状态
            timeIsShow = !timeIsShow;
        })

        // 猪场
        $('#pigFarm').on("click", function () {
            let angle = pigFarmIsShow ? "0deg" : "90deg"
            // 旋转图标
            $(this).find('i').css('rotate', angle)
            // 显示 下拉框
            $(".pigFarm").css("display", pigFarmIsShow ? 'none' : 'block')
            if (timeIsShow) {
                // 关闭旁边的下拉框
                $(".times").css("display", 'none')
                $('#times i').css('rotate', "0deg")
                timeIsShow = false
            }
            // 点击改变状态
            pigFarmIsShow = !pigFarmIsShow;
        })

        $('#notitem').on("click", function () {
            let date = $('#queryTime').text();
            window.location.href = `./notitem.html?bizdate=${date}`
        });
    }

})(window, document, jQuery)